package com.wang.controller.admin;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.wang.entity.User;
import com.wang.service.UserService;
import com.wang.util.JWTUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname AdminUserController
 * @Description TODO
 * @Date 2025/3/20 9:15
 * @Created by pc
 */
@RestController
@RequestMapping("/admin/user")
@Api(value = "后台用户接口",tags = "AdminUserController")
@CrossOrigin  // 拦截器将请求拦截下来 在请求到达控制器之前已经出现跨域问题了  利用过滤器优先于拦截器的机制去解决跨域问题
public class AdminUserController {

    @Resource
    private UserService userService;

    // @GetMapping
    // @ApiOperation(value = "查询所有用户",notes = "后台查询所有用户的详细信息")
    // public ResponseUtil findAll(){
    //     return userService.findAll();
    // }

    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation(value = "查询所有用户",notes = "后台分页查询所有用户的详细信息")
    public ResponseUtil find(@PathVariable("pageNum")Integer pageNum,
                             @PathVariable("pageSize")Integer pageSize){
        return userService.find(pageNum,pageSize);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "id查询用户",notes = "根据用户id查询用户具体信息")
    @ApiImplicitParam(value = "用户id",name = "id",required = true,dataType = "int",paramType = "path")
    public ResponseUtil findById(@PathVariable("id")Integer id){
        return userService.findById(id);
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = "后台管理员登录")
    @ApiImplicitParam(value = "用户id",name = "id",required = true,dataType = "int",paramType = "path")
    public Map<String,Object> adminLogin(@RequestBody User user){
        // ResponseUtil r = userService.adminLogin(user);
        Map<String,Object> map = new HashMap<>();
        // 响应状态码  响应数据  payload<String,Object>
        // 生成JWT
        ResponseUtil r = userService.adminLogin(user);
        if(r.getCode() == ResponseEnum.USER_ADMIN_LOGIN_SUCCESS.getCode()){
            // 登录成功
            Map<String,String> payload = new HashMap<>();
            User loginUser = (User) r.getData();
            payload.put("username",loginUser.getUsername());
            payload.put("userId",loginUser.getId().toString());
            payload.put("roleId",loginUser.getRole().toString());
            String token = JWTUtil.getToken(payload);
            map.put("status",true);
            map.put("msg",r.getMsg());
            map.put("token",token);
        }else {
            // 登录失败
            map.put("status",false);
            map.put("msg",r.getMsg());
        }
        return map;
    }

    // 验证token
    @GetMapping("/test")
    public Map<String,Object> test(String token, HttpRequest request){
        Map<String,Object> map = new HashMap<>();
        try {
            DecodedJWT verify = JWTUtil.verify(token);
            map.put("status", true);
            map.put("msg", "请求成功");
            return map;
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            map.put("msg", "无效签名!");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            map.put("msg", "token过期!");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            map.put("msg", "token算法不一致!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "token无效");
        }
        map.put("status", false);
        return map;
    }

    @GetMapping("/test2")
    public Map<String,Object> test(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","请求放行了");
        return map;
    }

}
